<template>
  <view></view>
</template>

<script lang="ts" setup>
import { defineProps, defineEmits } from 'vue'
import { useUserStore } from '../../store'
const currenLocation = ref()
const props = defineProps({
  address: {
    type: String,
  },
})

const getLocation = () => {
  uni.getLocation({
    type: 'gcj02',
    success: (res) => {
      const myloc = { latitude: res.latitude, longitude: res.longitude }
      useUserStore().setMyLocation(myloc)
      currenLocation.value = myloc
      console.log('定位成功', myloc)
      emit('locationSuccess', myloc)
      getLocationCityNameByGD()
    },
    fail: (res) => {
      console.log('定位失败')
    },
  })
}

const getLocationCityNameByTX = () => {
  uni.request({
    header: {
      'Content-Type': 'application/text',
    },
    url:
      'https://apis.map.qq.com/ws/geocoder/v1/?location=' +
      currenLocation.value.latitude +
      ',' +
      currenLocation.value.longitude +
      '&key=' +
      'KXVBZ-32BK7-HHNX3-HNFZ6-4YTAQ-KQBRA',
    success: (res) => {
      console.log('getLocationCityName-------', res)
      const data = JSON.parse(JSON.stringify(res))
      if (data.data.status === 0) {
        emit('getCityNameSuccess', data.data.result.address_component.city)
        useUserStore().setCityName(data.data.result.address_component.city)
      }
    },
  })
}

// 高德地图逆解析
const getLocationCityNameByGD = () => {
  uni.request({
    header: {
      'Content-Type': 'application/text',
    },
    url:
      'https://restapi.amap.com/v3/geocode/regeo?location=' +
      currenLocation.value.longitude +
      ',' +
      currenLocation.value.latitude +
      '&Key=' +
      '6b7937c357e8e7f98975a1cb5984fc81',
    success: (res) => {
      const data = JSON.parse(JSON.stringify(res))
      console.log('解析成功', data.data.infocode)
      if (data.data.infocode === '10000') {
        console.log('解析成功城市', data.data.regeocode.addressComponent.city)
        emit('getCityNameSuccess', data.data.regeocode.addressComponent.city)
        useUserStore().setCityName(data.data.regeocode.addressComponent.city)
      }
    },
  })
}

const setlocation = () => {
  console.log('定位检查')
  uni.authorize({
    scope: 'scope.userLocation',
    success() {
      // 1.1 允许授权
      getLocation()
    },
    fail() {
      // 1.2 拒绝授权
      uni.showModal({
        title: '是否授权当前位置',
        content: '需要获取您的地理位置，请确认授权，否则部分功能将无法使用',
        confirmText: '确认',
        cancelText: '取消',
        success: (res) => {
          if (res.confirm) {
            uni.openSetting({
              success: (res) => {
                getLocation()
              },
            })
          } else {
            // that.$u.toast('您已取消授权')
            return false
          }
        },
      })
      return false
    },
  })
}

// 导航到第三方地图
const toMapAPP = () => {
  uni.openLocation({
    latitude: currenLocation.value.latitude,
    longitude: currenLocation.value.longitude,
    address: 'suqian',
    success: function () {
      console.log('success')
    },
  })
}

const emit = defineEmits(['locationSuccess', 'getCityNameSuccess'])

defineExpose({ setlocation, toMapAPP, getLocationCityNameByGD })
</script>

<style lang="scss" scoped>
//
</style>
